package cn.cdutetc.foxwatch.serveice.imp;

import cn.cdutetc.foxwatch.serveice.FileService;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.UUID;

/**
 * @author 王勇
 * @ClassName： FileServiceImpl
 * @ClassPath： cn.cdutetc.foxwatch.serveice.imp.FileServiceImpl
 * @Description：
 * @date 2024/10/8 09:19
 */
@Service
public class FileServiceImpl implements FileService {
    public Boolean addFile(MultipartFile file) {
        if (file.isEmpty()) {
            return false;
        }
        try {
            String originalFilename = file.getOriginalFilename();
            String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
            String fileName = UUID.randomUUID().toString() + suffix;
            String savePath = ResourceUtils.getURL("upload").getPath();
            savePath = savePath + File.separator + suffix;
            file.transferTo(new File(savePath));
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}
